<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/3/21
 * Time: 9:18
 */

namespace app\api\controller;

use aldy\SmsDemo;
use think\Db;

class Login extends Base
{
    /**
     * 发送验证码
     * @return \think\response\Json|void
     * phone：填写的手机号
     * type：   1 注册   2 短信快捷登录||忘记密码  3 第三方登录时绑定手机号    其他传空
     * types:  当type为3时，传此值  1 微信  2 qq  3 微博
     * return:  0 请输入手机号   1 手机号格式不正确  2 该手机号已注册  3 没有此手机号的用户
     *          4 此号码已被绑定  5 短信验证码今日已发送5次，请明天再来  6 云之讯返回错误  7 操作数据库失败
     *          200  返回验证码
     */
    public function getPhoneCode()
    {
        $params = input('param.');
        if(trim($params['phone'], ' ') == '') {
            return json(['status' => 0, 'msg' => '请输入手机号']);
        }
        if(!preg_match("/^1[3456789]\d{9}$/", $params['phone'])) {
            return json(['status' => 0, 'msg' => '手机号格式不正确']);
        }
        $userInfo = Db::table('up_u_user')->where('user_phone', $params['phone'])->orderRaw('')->find();
        if(!empty($userInfo) && $params['type'] == 1) {//注册
            return json(['status' => 0, 'msg' => '该手机号已注册']);
        }
        if(empty($userInfo) && $params['type'] == 2) {//短信快捷登录||忘记密码
            return json(['status' => 0, 'msg' => '该账号不存在']);
        }
        if(!empty($userInfo) && $params['type'] == 3) {//第三方登录时绑定手机号
            if($params['types'] == 1) {
                $field = '_wx_';
            } elseif($params['types'] == 2) {
                $field = '_qq_';
            }
//            else {
//                $field = '_wb_';
//            }
            if($userInfo["user{$field}token"]) {
                return json(['status' => 0, 'msg' => '此号码已被绑定']);
            }
        }
        $startTime = strtotime(date('Y-m-d 00:00:00'));//当天的起始时间
        $endTime = strtotime(date('Y-m-d 23:59:59'));//当天的结束时间
        $where = [];
        $where['phone'] = $params['phone'];
        $where['time'] = ['between', [$startTime, $endTime]];
        $num = Db::table('up_phonecode')->where($where)->count('id');
        if($num == 5) {
            return json(['status' => 0, 'msg' => '短信验证码今日已发送5次，请明天再来']);
        }
        $code = GetRandCode();//6位数验证码
       // $sendres = $this->duaxin($params['phone'],$code);//发送验证码
        $templateid = config('yzx')['templateid'];
        $sendres = $this->alisms($params['phone'],$code,$templateid); //发送验证码
        if($sendres['Code'] != 'OK') {//发送不成功
            return json(['status' => 11, 'msg' => $sendres['Message']]);
        }else
//        if($sendres <0) {//发送不成功
//            return json(['status' => 0, 'msg' =>"发送失败"]);
//        } else
            {
            $data = [
                'phone' => $params['phone'],//发送的手机号
                'code' => $code,//验证码
                'time' => time(),//验证码发送的时间戳
                'date_time' => date('Y-m-d H:i:s')//日期格式
            ];
            $res = Db::table('up_phonecode')->insert($data);
            if(!$res) {
                return json(['status' => 0, 'msg' => '数据库操作失败']);
            }
            return json(['status' => 200, 'msg' => $code]);
        }
    }
    
    /**
     * 注册
     * @return bool
     * regist_type  注册类型  1 正常注册  2 第三方登录时没有注册过，直接注册登录
     * phone  手机号
     * code  验证码
     * password  密码
     * user_type  角色  1 学生  2 老师
     * grade_id  年级ID
     * third_type   第三方登录的种类   1 微信  2 qq  3 微博
     * name  第三方昵称
     * image  第三方头像
     * thirdToken  第三方token
     */
//    public function phoneRegister()
//    {
//        $phone = input('phone');//手机号
//        $token = md5(date('YmdHis') . rand(100000, 999999));
//
//        $data = [
//            'user_phone' => $phone,
//            'user_token' => $token,
//            'user_time' => date('Y-m-d H:i:s'),//注册时间
//        ];
//        if(input('regist_type') == 1){//正常注册
//            $code = input('code');//验证码
//            $password = trim(input('password'), ' ');//密码
//            if($phone == '' || $code == '' || $password == '') {
//                return json(['status' => 0, 'msg' => '有未填项，请检查']);
//            }
//            //验证手机号
//            if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
//                return json(['status' => 0, 'msg' => '请输入正确的手机号']);
//            }
//            $username = Db::table('up_u_user')->where('user_phone', $phone)->find();
//            if(!empty($username)) {
//                return json(['status' => 0, 'msg' => '此手机号码已注册']);
//            }
//            //验证验证码
//            $res = Db::table('up_phonecode')
//                ->where('phone', $phone)
//                ->order('time desc')
//                ->limit(1)
//                ->find();
//            if($code != $res['code']) {
//                return json(['status' => 0, 'msg' => '验证码错误']);
//            }
//            $now = time();
//            if($now - $res['time'] >= 1801) {
//                return json(['status' => 0, 'msg' => '验证码超时']);
//            }
//            if($res['status'] == 1) {
//                return json(['status' => 0, 'msg' => '验证码已使用']);
//            }
//            $data['user_password'] = md5($password);
//            $user_id = Db::name('u_user')->insertGetId($data);
//            //修改验证码状态
//            Db::table('up_phonecode')->where('id', $res['id'])->setField('status', 1);
//        }else{//第三方登录时注册
//            if(input('third_type') == 1) {
//                $field = '_wx_';
//            } elseif(input('third_type') == 2) {
//                $field = '_qq_';
//            } else {
//                $field = '_wb_';
//            }
//            $data["user{$field}token"] = input('thirdToken');
//            $data["user{$field}nickname"] = input('name');
//            $data["user{$field}image"] = input('image');
//            $user_id = Db::name('u_user')->insertGetId($data);
//            //更新用户头像、名称为第三方昵称、头像
//            $update['user_username'] = input('name');
//            $update['user_image'] = input('image');
//            if($user_id){
//                Db::table('up_u_user')->where('user_id', $user_id)->update($update);
//            }
//        }
//
//        if($user_id) {
//            //注册成功后自动登录
//            $userInfo = Db::table('up_u_user')
//                ->where('user_id', $user_id)
//                ->find();
//            return json(['status' => 200, 'msg' => $userInfo]);
//        } else {
//            return json(['status' => 0, 'msg' => '注册失败']);
//        }
//    }
    
    /**
     * 用户正常注册
     * @return \think\response\Json
     * phone    手机号
     * code     验证码
     * password 密码
     */
    public function userRegist(){
        $phone = trim(input('phone'), ' ');
        $code = input('code');
        $password = trim(input('password'), ' ');
        
        if($phone == '' || $code == '' || $password == '') {
            return json(['status' => 0, 'msg' => '有未填项，请检查']);
        }
        
        //验证手机号
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
        }
        $username = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(!empty($username)) {
            return json(['status' => 0, 'msg' => '此手机号码已注册']);
        }
        
        //验证验证码
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        $now = time();
        if($now - $res['time'] >= 601) {
            return json(['status' => 0, 'msg' => '验证码超时']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
        
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $data['user_password'] = md5($password);
        $data['user_phone'] = $phone;
        $data['user_time'] = date('Y-m-d H:i:s');
        $data['user_token'] = $token;
        $user_id = Db::name('u_user')->insertGetId($data);
        if($user_id){
            //修改验证码状态
            Db::table('up_phonecode')->where('id', $res['id'])->setField('status', 1);
            //注册成功后自动登录
            $userInfo = Db::table('up_u_user')->where('user_id', $user_id)->find();
//            $rytoken = $this->rongyun($userInfo['user_id'],'who',$userInfo['user_image']);
//            $userInfo['rytoken'] = $rytoken['token'];
            return json(['status' => 200, 'msg' => $userInfo]);
        }else{
            return json(['status' => 0, 'msg' => '注册失败']);
        }
    }
    
    /**
     * 用户第三方登录时注册
     * @return \think\response\Json
     * phone          手机号
     * code           验证码
     * thirdToken     第三方token
     * name           第三方昵称
     * image          第三方头像
     * third_type     第三方类型   1 微信  2 qq  3 微博
     *
     */
    public function thirdRegist(){
        $phone = trim(input('phone'), ' ');
        $code = input('code');
//        $password = trim(input('password'), ' ');
        if(input('third_type') == 1) {
            $field = '_wx_';
        } elseif(input('third_type') == 2) {
            $field = '_qq_';
        }

        if($phone == '' || $code == '') {
            return json(['status' => 0, 'msg' => '有未填项，请检查']);
        }
        //验证手机号
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
        }
        $username = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(!empty($username)) {
            return json(['status' => 0, 'msg' => '此手机号码已注册']);
        }

        //验证验证码
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        $now = time();
        if($now - $res['time'] >= 601) {
            return json(['status' => 0, 'msg' => '验证码超时']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }

        $token = md5(date('YmdHis') . rand(100000, 999999));
//        $data['user_password'] = md5($password);
        $data['user_phone'] = $phone;
        $data['user_time'] = date('Y-m-d H:i:s');
        $data['user_token'] = $token;
        $data["user{$field}token"] = input('thirdToken');
        $data["user{$field}nickname"] = input('name');
        $data["user{$field}image"] = input('image');
        //更新用户头像、名称为第三方昵称、头像
        $data['user_username'] = input('name');
        $data['user_image'] = input('image');
        $user_id = Db::name('u_user')->insertGetId($data);

        if($user_id){
            //修改验证码状态
            Db::table('up_phonecode')->where('id', $res['id'])->setField('status', 1);
            //注册成功后自动登录
            $userInfo = Db::table('up_u_user')->where('user_id', $user_id)->find();
            return json(['status' => 200, 'msg' => $userInfo]);
        }else{
            return json(['status' => 0, 'msg' => '注册失败']);
        }
    }
    

    /**
     * 手机号+密码登录
     * @return bool
     * phone  手机号
     * password  密码
     */
    public function userLogin()
    {
        $phone = input('phone');
        $password = trim(input('password'));
        if($phone == '') {
            return json(['status' => 0, 'msg' => '请输入手机号']);
        }
        if($password == '') {
            return json(['status' => 0, 'msg' => '请输入密码']);
        }
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
        }
        $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($userInfo)) {
            return json(['status' => 0, 'msg' => '该账号不存在']);
        }
        if(md5($password) != $userInfo['user_password']) {
            return json(['status' => 0, 'msg' => '密码错误']);
        }
        if($userInfo['user_status'] == 3) {
            return json(['status' => 0, 'msg' => '该账号已被冻结']);
        }
        if($userInfo['user_status'] == 4) {
            return json(['status' => 0, 'msg' => '该账号已被删除，请联系管理员']);
        }
        
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $update = [
            'user_token' => $token,
        ];
        $result = Db::table('up_u_user')->where('user_phone', $phone)->update($update);
            if($result) {
                $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
                //先倒叙查询这个用户的退出时间
                $user1 = Db::table('up_user_login_time')->where('user_id', $user['user_id'])->order('user_login_id desc')->find();
                if($user1['user_login_id']!=''){
                    if($user1['user_login_ent_time']==''||$user1['user_login_ent_time']==null){
                        $dataList1['user_login_ent_time'] =date('Y-m-d H:i:s',time());
                         Db::table('up_user_login_time')
                            ->where('user_login_id', $user1['user_login_id'])
                            ->update($dataList1);
                    }
                }

                $dataList['user_id'] = $user['user_id'];
                $dataList['user_login_start_time'] =date('Y-m-d H:i:s',time());
                Db::table('up_user_login_time')->insert($dataList);


                return json(['status' => 200, 'msg' => $user]);
            } else {
            return json(['status' => 0, 'msg' => '登录失败']);
        }
        
    }
    
    /**
     * 短信快捷登录
     * @return bool
     * phone  手机号
     * code  验证码
     */
//    public function phoneLogin()
//    {
//        $phone = trim(input('phone'), ' ');
//        $code = trim(input('code'), ' ');
//
//        if($phone == '') {
//            return json(['status' => 0, 'msg' => '请输入手机号码']);
//        }
//        if($code == '') {
//            return json(['status' => 0, 'msg' => '请输入验证码']);
//        }
//        if(!preg_match("/^1[34578]\d{9}$/", $phone)) {
//            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
//        }
//        $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
//        if(empty($userInfo)) {
//            return json(['status' => 0, 'msg' => '该账号不存在']);
//        }
//
//        $res = Db::table('up_phonecode')
//            ->where('phone', $phone)
//            ->order('time desc')
//            ->limit(1)
//            ->find();
//        if($code != $res['code']) {
//            return json(['status' => 0, 'msg' => '验证码错误']);
//        }
//        $now = time();
//        if($now - $res['time'] >= 1801) {
//            return json(['status' => 0, 'msg' => '验证码超时']);
//        }
//        if($res['status'] == 1) {
//            return json(['status' => 0, 'msg' => '验证码已使用']);
//        }
//
//        if($userInfo['user_status'] == 3) {
//            return json(['status' => 0, ' msg' => '该账号已被冻结']);
//        }
//        if($userInfo['user_status'] == 4) {
//            return json(['status' => 0, ' msg' => '该账号已被删除，请联系管理员']);
//        }
//        //更新token
//        $token = md5(date('YmdHis') . rand(100000, 999999));
//        $update = [
//            'user_token' => $token,
//        ];
//        $result = Db::table('up_u_user')->where('user_phone', $phone)->update($update);
//        if($result) {
//            $username = Db::table('up_u_user')->where('user_phone', $phone)->find();
//            Db::table('up_phonecode')->where('id', $res['id'])->update(['status' => 1]);
//            //如果没有选择角色，则跳转到选择角色页面
//            if(empty($username['user_type'])){
//                return json(['status' => 400, 'msg' => $username]);
//            }
////            $rytoken = $this->rongyun($username['user_id'],'who',$username['user_image']);
////            $username['rytoken'] = $rytoken['token'];
////            $username['user_f_online'] = 1;
//            return json(['status' => 200, 'msg' => $username]);
//        } else {
//            return json(['status' => 0, 'msg' => '登录失败']);
//        }
//    }
    
    /**
     * 微信||QQ||微博
     * thirdToken  第三方token
     * phone  手机号
     * code  验证码
     * name  第三方昵称
     * image  第三方头像
     * type  第三方类型   1 微信  2 qq
     * @return string
     */
    public function thirdLogin()
    {
        $thirdToken = input('thirdToken');
        $type = input('type');
        if($type == 1) {
            $field = '_wx_';
        } elseif($type == 2) {
            $field = '_qq_';
        }
        $where["user{$field}token"] = $thirdToken;
        $isFirstLogin = Db::table('up_u_user')->where($where)->find();
        //---------------是否是第一次使用同类型的第三方登录-------
        if($isFirstLogin) {//已经用同类第三方登录过，直接登录
             if($isFirstLogin['user_status'] == 3) {
                return json(['status' => 0, ' msg' => '该账号已被冻结']);
            }
            if($isFirstLogin['user_status'] == 4) {
                return json(['status' => 0, ' msg' => '该账号已被删除，请联系管理员']);
            }
            if($isFirstLogin['user_phone']!=''){//绑定过手机号码
                //更新token
                $token = md5(date('YmdHis') . rand(100000, 999999));
                $update = [
                    'user_token' => $token,
                ];
                Db::table('up_u_user')->where($where)->update($update);

                $isFirstLogin = Db::table('up_u_user')->where($where)->find();
                return json(['status' => 200, 'msg' => $isFirstLogin]);
            }else{//没有绑定手机号码
                return json(['status' => 300, 'msg' => '您未曾绑定手机号，建议您先绑定']);
            }
        }else{
            return json(['status' => 300, 'msg' => '']);
        }


    }
    public function bangding(){
        $thirdToken = input('thirdToken');
        $type = input('type');
        if($type == 1) {
            $field = '_wx_';
        } elseif($type == 2) {
            $field = '_qq_';
        }
            $name = input('name');
            $image = input('image');
            $phone = trim(input('phone'), ' ');
            $code = trim(input('code'), ' ');
            $token = md5(date('YmdHis') . rand(100000, 999999));
            if($code == '' || $phone == '') {
                return json(['status' => 0, 'msg' => '有未填项，请检查']);
            }
            if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
                return json(['status' => 0, 'msg' => '请填写正确的手机号码']);
            }

            $res = Db::table('up_phonecode')
                ->where('phone', $phone)
                ->order('time desc')
                ->limit(1)
                ->find();
            if($code != $res['code']) {
                return json(['status' => 0, 'msg' => '验证码错误']);
            }
            $now = time();
            if($now - $res['time'] >= 1801) {
                return json(['status' => 0, 'msg' => '验证码失效']);
            }
            if($res['status'] == 1) {
                return json(['status' => 0, 'msg' => '验证码已使用']);
            }

            $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
            if(empty($userInfo)) {
//                return json(['status' => 100, 'msg' => '']);
                $date = [
                    'user_token' => $token,
                    'user' . $field . 'token' => $thirdToken,
                    'user' . $field . 'nickname' => $name,
                    'user' . $field . 'image' => $image,
                    'user_phone' => $phone
                ];
                 Db::table('up_u_user')->insertGetId($date);
            }else{
                $date = [
                    'user_token' => $token,
                    'user' . $field . 'token' => $thirdToken,
                    'user' . $field . 'nickname' => $name,
                    'user' . $field . 'image' => $image,
                ];
               Db::table('up_u_user')->where('user_phone', $phone)->update($date);
            }

            //如果用户没有昵称、头像信息，则更新为第三方昵称、头像
            if(!$userInfo['user_username']) {
                $update['user_username'] = $name;
            }
            if(!$userInfo['user_image']) {
                $update['user_image'] = $image;
            }
            if(!$userInfo['user_username']&&!$userInfo['user_image']){
                $res1 = Db::table('up_u_user')->where('user_phone', $phone)->update($update);//更新用户信息
            }
//            if($res1) {
                $userData = Db::table('up_u_user')->where('user_phone', $phone)->find();
                Db::table('up_phonecode')->where('id', $res['id'])->update(['status' => 1]);//改变验证码状态
//                $rytoken = $this->rongyun($userData['user_id'],'who',$userData['user_image']);
//                $userData['rytoken'] = $rytoken['token'];
//                $userData['user_f_online'] = 1;
                return json(['status' => 200, 'msg' => $userData]);
//            }
//            else {
//                return json(['status' => 0, 'msg' => '绑定失败']);
//            }

    }
    /**
     * 忘记密码第一步
     * @return bool
     * phone  手机号
     * code  验证码
     */
//    public function forgetFirst()
//    {
//        $phone = trim(input('phone'), ' ');
//        $code = trim(input('code'), ' ');
//
//        if($phone == '') {
//            return json(['status' => 0, 'msg' => '请输入手机号码']);
//        }
//        if($code == '') {
//            return json(['status' => 0, 'msg' => '请输入验证码']);
//        }
//        if(!preg_match("/^1[34578]\d{9}$/", $phone)) {
//            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
//        }
//        $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
//        if(empty($userInfo)) {
//            return json(['status' => 0, 'msg' => '该账号不存在']);
//        }
//
//        $res = Db::table('up_phonecode')
//            ->where('phone', $phone)
//            ->order('time desc')
//            ->limit(1)
//            ->find();
//        if($code != $res['code']) {
//            return json(['status' => 0, 'msg' => '验证码错误']);
//        }
//        $now = time();
//        if($now - $res['time'] >= 601) {
//            return json(['status' => 0, 'msg' => '验证码超时']);
//        }
//        if($res['status'] == 1) {
//            return json(['status' => 0, 'msg' => '验证码已使用']);
//        }
//
//        if($userInfo['user_status'] == 3) {
//            return json(['status' => 0, ' msg' => '该账号已被冻结']);
//        }
//        if($userInfo['user_status'] == 4) {
//            return json(['status' => 0, ' msg' => '该账号已被删除，请联系管理员']);
//        }
//        Db::table('up_phonecode')->where('id', $res['id'])->update(['status' => 1]);
//        $username = Db::table('up_u_user')->where('user_phone', $phone)->find();
//        return json(['status' => 200, 'msg' => $username]);
//    }
    
    /**
     * 修改密码
     * @return \think\response\Json
     * new_password  新密码
     * verify_password  确认密码
     * user_id  用户ID
     */
    public function changePassword(){
        $new_password = trim(input('password'), ' ');
        $user_id = input('user_id');
        $phone = trim(input('phone'), ' ');
        $code = trim(input('code'), ' ');

        if($phone == '') {
            return json(['status' => 0, 'msg' => '请输入手机号码']);
        }
        if(!preg_match("/^1[34578]\d{9}$/", $phone)) {
            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
        }
        if($code == '') {
            return json(['status' => 0, 'msg' => '请输入验证码']);
        }
        if($new_password == ''){
            return json(['status' => 0, 'msg' => '请填写新密码']);
        }
        $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($userInfo)) {
            return json(['status' => 0, 'msg' => '该账号不存在']);
        }

        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        $now = time();
        if($now - $res['time'] >= 601) {
            return json(['status' => 0, 'msg' => '验证码超时']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }

        if($userInfo['user_status'] == 3) {
            return json(['status' => 0, ' msg' => '该账号已被冻结']);
        }
        if($userInfo['user_status'] == 4) {
            return json(['status' => 0, ' msg' => '该账号已被删除，请联系管理员']);
        }
        Db::table('up_phonecode')->where('id', $res['id'])->update(['status' => 1]);
        
//        $update['user_password'] = md5($new_password);
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $update = [
            'user_token' => $token,
            'user_password' => md5($new_password)
        ];
        $res = Db::name('u_user')->where('user_id', $userInfo['user_id'])->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '修改成功']);
        }else{
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 修改手机号第一步
     * @return \think\response\Json
     * phone  用户手机号
     * code  验证码
     */
//    public function changePhoneF(){
//        $phone = input('phone');
//        $code = input('code');
//
//        if($code == '') {
//            return json(['status' => 0, 'msg' => '请输入验证码']);
//        }
//        $res = Db::table('up_phonecode')
//            ->where('phone', $phone)
//            ->order('time desc')
//            ->limit(1)
//            ->find();
//        if($code != $res['code']) {
//            return json(['status' => 0, 'msg' => '验证码错误']);
//        }
//        $now = time();
//        if($now - $res['time'] >= 601) {
//            return json(['status' => 0, 'msg' => '验证码超时']);
//        }
//        if($res['status'] == 1) {
//            return json(['status' => 0, 'msg' => '验证码已使用']);
//        }
//        $result = Db::table('up_phonecode')->where('id', $res['id'])->update(['status' => 1]);
//
//        if($result){
//            return json(['status' => 200, 'msg' => '']);
//        }else{
//            return json(['status' => 0, 'msg' => '操作数据库错误']);
//        }
//    }
    
    /**
     * 修改手机号第二步
     * @return \think\response\Json
     * user_id  用户ID
     * phone  用户手机号
     * code  验证码
     */
//    public function changePhoneS(){
//        $phone = input('phone');
//        $code = input('code');
//        $user_id = input('user_id');
//
//        if($phone == '') {
//            return json(['status' => 0, 'msg' => '请输入手机号码']);
//        }
//
//        if($code == '') {
//            return json(['status' => 0, 'msg' => '请输入验证码']);
//        }
//
//        if(!preg_match("/^1[34578]\d{9}$/", $phone)) {
//            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
//        }
//
//        $info = Db::table('up_u_user')->where('user_phone', $phone)->find();
//        if($info){
//            return json(['status' => 0, 'msg' => '该手机号已注册']);
//        }
//
//        $res = Db::table('up_phonecode')
//            ->where('phone', $phone)
//            ->order('time desc')
//            ->limit(1)
//            ->find();
//        if($code != $res['code']) {
//            return json(['status' => 0, 'msg' => '验证码错误']);
//        }
//        $now = time();
//        if($now - $res['time'] >= 601) {
//            return json(['status' => 0, 'msg' => '验证码超时']);
//        }
//        if($res['status'] == 1) {
//            return json(['status' => 0, 'msg' => '验证码已使用']);
//        }
//
//        $result = Db::table('up_u_user')->where('user_id', $user_id)->update(['user_phone' => $phone]);
//
//        if($result){
//            Db::table('up_phonecode')->where('id', $res['id'])->update(['status' => 1]);
//            return json(['status' => 200, 'msg' => '修改成功']);
//        }else{
//            return json(['status' => 0, 'msg' => '修改失败']);
//        }
//    }
    /*
     * alisms 阿里大于验证码
     * @param $phoneNum 手机号
     * @param $code验证码
     */
  public  function alisms($phoneNum,$code,$templateid){
        header('Content-Type: text/plain; charset=utf-8');
        $Alidy = new SmsDemo();
        $result = $Alidy->sendSms($phoneNum,config('alidy')['SignName'],$templateid,$code);
        $result = object_array($result);
        return $result;
    }
    public function duaxin($phone,$code)
    {
        $pwd = strtoupper(md5('SDK-NSQ-010-00074'.'ylwOm33e'));
        $url = 'http://sdk2.entinfo.cn:8061/mdsmssend.ashx?sn=SDK-NSQ-010-00074&pwd='.$pwd.'&mobile='.$phone.'&content=【戏嗨】你的登录验证码'.$code.',10分钟内有效,请勿泄露给他人&ext=&stime=&rrid=&msgfm=';
        $header = array(
            'Accept: application/json',
        );
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 超时设置,以秒为单位
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);

        // 超时设置，以毫秒为单位
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

        // 设置请求头
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        //执行命令
        $data = curl_exec($curl);
        return $data;
    }
}